package com.piedra.image;

import java.io.File;

public class ImageChangeNameUtil {
	
	/**
	 * 将新的图片和当前路径的第2张 交换
	 * 正常图片名称如： g:/abc/def/gg-2.jpg
	 * 换名规则仅对地图(项目)有效,其他程序要使用需作出相应的修改 
	 * @param rootPath  g:/abc/def
	 * @param fileName  gg
	 * @param imageIndex 2
	 * @author LINWENBIN
	 * @date 2013-8-9
	 */
	public void changeName(String rootPath,String fileName, int imageIndex){
		
		String newMainFileName = rootPath + "/"+ fileName+ "-"+imageIndex+".jpg";
		String tempFileName = rootPath + "/"+fileName+".jpg";
		String oldMainFileName = rootPath + "/"+fileName+"-2.jpg";
		
		File newMainFile = new File(newMainFileName);
		File tempFile = new File(tempFileName);
		newMainFile.renameTo(tempFile);
		
		File oldMainFile = new File(oldMainFileName);
		tempFile = new File(newMainFileName);
		oldMainFile.renameTo(tempFile);
		
		tempFile = new File(oldMainFileName);
		File mainFile = new File(tempFileName);
		mainFile.renameTo(tempFile);
		
		mainFile = null;
		tempFile = null;
		oldMainFile = null;
		newMainFile = null;
	}
}
